package com.structural.composite.example.service.engine;

import com.structural.composite.example.model.aggregates.TreeRich;
import com.structural.composite.example.model.vo.EngineResult;
import com.structural.composite.example.model.vo.TreeNode;
import com.structural.composite.example.model.vo.TreeRoot;
import com.structural.composite.example.service.logic.LogicFilter;

import java.util.Map;

/**
 * <p></p>
 *
 * @author jiuhua.xu
 * @version 1.0
 * @since JDK 1.8
 */
public abstract class EngineBase extends EngineConfig implements IEngine{

    @Override
    public abstract EngineResult process(Long treeId, String userId, TreeRich treeRich, Map<String, String> decisionMatter);

    protected TreeNode engineDecisionMaker(TreeRich treeRich, Long treeId, String userId, Map<String, String> decisionMatter) {
        final TreeRoot treeRoot = treeRich.getTreeRoot();
        final Map<Long, TreeNode> treeNodeMap = treeRich.getTreeNodeMap();

        final Long rootNodeId = treeRoot.getTreeRootNodeId();
        TreeNode treeNodeInfo = treeNodeMap.get(rootNodeId);
        while (treeNodeInfo.getNodeType().equals(1)) {
            final String ruleKey = treeNodeInfo.getRuleKey();
            final LogicFilter logicFilter = logicFilterMap.get(ruleKey);
            final String matterValue = logicFilter.matterValue(treeId, userId, decisionMatter);
            final Long nextNode = logicFilter.filter(matterValue, treeNodeInfo.getTreeNodeLinkList());
            treeNodeInfo = treeNodeMap.get(nextNode);
            System.out.printf("决策树引擎=>%s userId：%s treeId：%s treeNode：%s ruleKey：%s matterValue：%s%n", treeRoot.getTreeName(), userId,
                    treeId, treeNodeInfo.getTreeNodeId(), ruleKey, matterValue);
        }


        return treeNodeInfo;
    }
}
